package com.esun.payment_api.controller;

import com.esun.common.api.CommonResult;
import com.esun.common.bean.PageRequest;
import com.esun.common.utils.AESUtils;
import com.esun.common.utils.SecurityUtils;
import com.esun.common.utils.StringUtils;
import com.esun.payment_api.bean.UserBean;
import com.esun.payment_api.domain.User;
import com.esun.payment_api.security.model.LoginByOpenIdBody;
import com.esun.payment_api.service.LogService;
import com.esun.payment_api.service.LoginService;
import com.esun.payment_api.service.UserService;
import com.esun.payment_api.security.AjaxResult;
import com.esun.payment_api.security.model.LoginBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;

@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    LoginService loginService;

    @Autowired
    UserService userService;

    @Autowired
    LogService logService;

    final String moduleName = "登陆模块";

    @PostMapping("/loginByPassword")
    public CommonResult<UserBean> loginByPassword(@RequestBody LoginBody loginBody){
        CommonResult<UserBean> userBeanCommonResult = loginService.loginByPassword(loginBody);
        logService.writeLoginLog(userBeanCommonResult.getData(), userBeanCommonResult);
        //维护调试
//        if("dfcxadmin".equals(loginBody.getUsername())){
//            return userBeanCommonResult;
//        }else {
//            return CommonResult.failed("维护中...");
//        }
        return userBeanCommonResult;
    }

    @PostMapping("/loginByOpenId")
    public CommonResult<UserBean> loginByOpenId(@RequestBody LoginByOpenIdBody loginByOpenIdBody){
        CommonResult<UserBean> userBeanCommonResult = loginService.loginByOpenId(loginByOpenIdBody);
        logService.writeLoginLog(userBeanCommonResult.getData(), userBeanCommonResult);
//        return CommonResult.failed("维护中...");
        return userBeanCommonResult;
    }

    @PostMapping("/registerTest")
    public CommonResult registerTest(String username, String password){
        return loginService.registerTest(username, SecurityUtils.encryptPassword(password));
    }

    @PostMapping("/loginTest")
    public CommonResult loginTest(String username, String password){
        try{
            password = AESUtils.encrypt(password);
            LoginBody loginBody = new LoginBody();
            loginBody.setUsername(username);
            loginBody.setPassword(password);
            return loginByPassword(loginBody);
        } catch (Exception e){
            return CommonResult.failed("密码加密错误，参考原因"+e.getMessage());
        }
    }

    private String getNowIP4() throws IOException {
        String ip = null;
        String objWebURL = "https://bajiu.cn/ip/";
        BufferedReader br = null;
        try {
            URL url = new URL(objWebURL);
            br = new BufferedReader(new InputStreamReader(url.openStream()));
            String s = "";
            String webContent = "";
            while ((s = br.readLine()) != null) {
                if (s.indexOf("互联网IP") != -1) {
                    ip = s.substring(s.indexOf("'") + 1, s.lastIndexOf("'"));
                    break;
                }
            }
        } finally {
            if (br != null)
                br.close();
        }
        if (StringUtils.isEmpty(ip)) {
            throw new RuntimeException();
        }
        return ip;
    }

}
